package com.zbkj.front.controller;


import cn.hutool.core.util.ObjectUtil;
import com.zbkj.dal.common.exception.CrmebException;
import com.zbkj.dal.entity.coupon.Coupon;
import com.zbkj.dal.entity.product.ProductTab;
import com.zbkj.dal.entity.seckill.SeckillProduct;
import com.zbkj.dal.entity.system.SystemConfig;
import com.zbkj.dal.base.page.CommonPage;
import com.zbkj.dal.pojo.request.PageParamRequest;
import com.zbkj.dal.pojo.response.*;
import com.zbkj.dal.base.result.CommonResult;
import com.zbkj.dal.pojo.vo.IndexTabListVO;
import com.zbkj.dal.pojo.vo.ProCategoryCacheVo;
import com.zbkj.front.model.vo.UserSearchVO;
import com.zbkj.front.service.IndexService;
import com.zbkj.service.service.ProductCategoryService;
import com.zbkj.service.service.ProductTabService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * 首页控制器
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/front/index")
@Api(tags = "首页控制器")
public class IndexController {

    @Autowired
    private IndexService indexService;
    @Autowired
    private ProductCategoryService productCategoryService;
    @Autowired
    private ProductTabService productTabService;

    @ApiOperation(value = "首页数据")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<IndexInfoResponse> getIndexInfo() {
        return CommonResult.success(indexService.getIndexInfo());
    }

    @ApiOperation(value = "首页商品列表")
    @RequestMapping(value = "/product/list", method = RequestMethod.GET)
    @ApiImplicitParam(name = "cid", value = "一级商品分类id，全部传0", required = true)
    public CommonResult<CommonPage<ProductCommonResponse>> getProductList(@RequestParam(value = "cid") Integer cid,
                                                                          PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(indexService.findIndexProductList(cid, pageParamRequest)));
    }

    @ApiOperation(value = "首页店铺列表-根据数量加载")
    @RequestMapping(value = "/merchant/list/{recomdnum}", method = RequestMethod.GET)
    public CommonResult<List<IndexMerchantResponse>> getMerchantListByRecomdNum(@PathVariable(name = "recomdnum", required = false) Integer recomdnum) {
        return CommonResult.success(indexService.findIndexMerchantListByRecomdNum(recomdnum));
    }

    @ApiOperation(value = "首页店铺列表-根据id集合加载")
    @RequestMapping(value = "/merchant/listbyids/{ids}", method = RequestMethod.GET)
    public CommonResult<List<IndexMerchantResponse>> getMerchantListByIds(@PathVariable(name = "ids", required = false) String ids) {
        return CommonResult.success(indexService.findIndexMerchantListByIds(ids));
    }

    @ApiOperation(value = "热门搜索", tags = "搜索页")
    @RequestMapping(value = "/search/keyword", method = RequestMethod.GET)
    public CommonResult<List<HashMap<String, Object>>> hotKeywords() {
        return CommonResult.success(indexService.hotKeywords());
    }

    @ApiOperation(value = "颜色配置")
    @RequestMapping(value = "/color/config", method = RequestMethod.GET)
    public CommonResult<SystemConfig> getColorConfig() {
        return CommonResult.success(indexService.getColorConfig());
    }

    @ApiOperation(value = "全局本地图片域名")
    @RequestMapping(value = "/image/domain", method = RequestMethod.GET)
    public CommonResult<String> getImageDomain() {
        return CommonResult.success(indexService.getImageDomain());
    }

    @ApiOperation(value = "版权图片")
    @RequestMapping(value = "/copyright/company/image", method = RequestMethod.GET)
    public CommonResult<Object> getCopyrightCompanyImage() {
        return CommonResult.success(indexService.getCopyrightCompanyImage());
    }

    @ApiOperation(value = "首页秒杀信息")
    @RequestMapping(value = "/seckill/info", method = RequestMethod.GET)
    public CommonResult<List<SeckillProduct>> getIndexSeckillInfo() {
        return CommonResult.success(indexService.getIndexSeckillInfo());
    }

    @ApiOperation(value = "首页优惠券")
    @RequestMapping(value = "/coupon/info/{limit}", method = RequestMethod.GET)
    public CommonResult<List<Coupon>> getIndexCouponInfo(@PathVariable(value = "limit") Integer limit) {
        if (ObjectUtil.isNull(limit) || limit <= 0) throw new CrmebException("limit参数不合法");
        if (limit >= 8) throw new CrmebException("首页 组件 优惠券上限不得超过 8 条");
        return CommonResult.success(indexService.getIndexCouponInfo(limit));
    }

    @ApiOperation(value = "获取系统时间")
    @RequestMapping(value = "/get/system/time", method = RequestMethod.GET)
    public CommonResult<Long> getSystemTime() {
        return CommonResult.success(System.currentTimeMillis());
    }

    @ApiOperation(value = "获取底部导航信息")
    @RequestMapping(value = "/get/bottom/navigation", method = RequestMethod.GET)
    public CommonResult<PageLayoutBottomNavigationResponse> getBottomNavigation() {
        return CommonResult.success(indexService.getBottomNavigationInfo());
    }

    @ApiOperation(value = "获取版本信息")
    @RequestMapping(value = "/index/get/version", method = RequestMethod.GET)
    public CommonResult<AppVersionResponse> getVersion() {
        return CommonResult.success(indexService.getVersion());
    }


    @ApiOperation(value = "获取引导页信息")
    @RequestMapping(value = "/get/splash", method = RequestMethod.GET)
    public CommonResult<SplashResponse> getSplash() {
        return CommonResult.success(indexService.getSplash());
    }

    // ----------------------------- NEW -----------------------------

    @GetMapping(value = "/v2/user/search/keyword")
    @ApiOperation(value = "用户搜索历史", tags = "搜索页")
    public CommonResult<List<UserSearchVO>> userHotKeywords() {
        return CommonResult.success(indexService.userHotKeywords());
    }

    @DeleteMapping(value = "/v2/user/search/keyword")
    @ApiOperation(value = "用户搜索历史删除", tags = "搜索页")
    public CommonResult<Integer> userHotKeywordsDel(@RequestParam("ids") List<Integer> ids) {
        return CommonResult.success(indexService.userHotKeywordsDel(ids));
    }

    @GetMapping(value = "/product/category")
    @ApiOperation(value = "分类页面", tags = "分类页面")
    public CommonResult<List<ProCategoryCacheVo>> category() {
        List<ProCategoryCacheVo> levelCategory = productCategoryService.getLevelCategory(1);
        ProCategoryCacheVo proCategoryCacheVo = new ProCategoryCacheVo();
        proCategoryCacheVo.setId(0);
        proCategoryCacheVo.setName("品牌");
        proCategoryCacheVo.setLevel(1);
        proCategoryCacheVo.setSort(0);
        proCategoryCacheVo.setIsShow(true);
        proCategoryCacheVo.setChildList(Collections.emptyList());
        levelCategory.add(0, proCategoryCacheVo);
        return CommonResult.success(levelCategory);
    }

    @GetMapping(value = "/product/tab/list")
    @ApiOperation(value = "首页展示Tab列表设置", tags = "分类页面")
    public CommonResult<IndexTabListVO> tabList() {
        // 通过 view字段查找ProductTab中的指定数据
        List<ProductTab> productTabs = productTabService.findProductTabByViewIn(IndexTabListVO.TabView.allCodes);
        // 第一个
        IndexTabListVO indexTabListVO = new IndexTabListVO();
        productTabs.forEach(item -> {
            if (StringUtils.equals(item.getView(), IndexTabListVO.TabView.NEW_FASHIONABLE.getCode())) {
                IndexTabListVO.TabCodeView tabCodeView1 = new IndexTabListVO.TabCodeView();
                tabCodeView1.setTabName(IndexTabListVO.TabView.NEW_FASHIONABLE.getName());
                tabCodeView1.setTabCode(item.getTabCode());
                tabCodeView1.setLimit(item.getSize());
                indexTabListVO.setNewFashionable(tabCodeView1);
            } else if (StringUtils.equals(item.getView(), IndexTabListVO.TabView.RECOMMENDED_FINEST.getCode())) {
                IndexTabListVO.TabCodeView tabCodeView2 = new IndexTabListVO.TabCodeView();
                tabCodeView2.setTabName(IndexTabListVO.TabView.RECOMMENDED_FINEST.getName());
                tabCodeView2.setTabCode(item.getTabCode());
                tabCodeView2.setLimit(item.getSize());
                indexTabListVO.setRecommendedFinest(tabCodeView2);
            }
        });
        return CommonResult.success(indexTabListVO);
    }
}



